package ${package_name}.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.chdz.common.service.${model_name}Service;
import com.chdz.common.request.Add${model_name}Request;
import com.chdz.common.request.Update${model_name}Request;
import org.springframework.web.bind.annotation.PathVariable;
import javax.servlet.http.HttpServletRequest;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import javax.validation.Valid;
import com.chdz.common.model.SysMessage;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestParam;
import com.chdz.common.constant.CommonStatus;
import com.chdz.common.response.CreatedResponse;
import com.chdz.common.response.MsgResponse;
import com.chdz.common.response.${model_name}Response;

/**
 * @Description ${table_annotation}
 * @author ${author}
 * @date ${date}
 */
@Api(tags = "${table_annotation}", description = "${table_annotation}Api接口")
@Controller
public class ${model_name}Controller {

    private final static Logger logger = LoggerFactory.getLogger(${model_name}Controller.class);

    @Autowired
    private ${model_name}Service ${model_name?uncap_first}Service;


    @ApiOperation(value = "添加${table_annotation}", notes = "添加${table_annotation}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "${model_name?uncap_first}Request",value ="${table_annotation}对象",required = true,dataType = "Add${model_name}Request")
    })
    @ApiResponses({ @ApiResponse(code = CommonStatus.CREATED, message = "Created", response = CreatedResponse.class),
			@ApiResponse(code = CommonStatus.ACCEPTED, message = "Accepted", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.BAD_REQUEST, message = "Bad Request", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.INTERNAL_SERVER_ERROR, message = "Server Error", response = MsgResponse.class) })
    @RequestMapping(value = "/${model_name?lower_case}s",
            method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<?> add${model_name}(@Valid @RequestBody Add${model_name}Request ${model_name?uncap_first}Request,BindingResult bindingResult, HttpServletRequest request){
        if (bindingResult.hasErrors()){
            return  new ResponseEntity<>(
                new SysMessage(
                    bindingResult.getFieldError().getDefaultMessage()),HttpStatus.BAD_REQUEST);
        }
        try {
            return ${model_name?uncap_first}Service.add${model_name}(${model_name?uncap_first}Request, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
			return new SysMessage(e).buildErrorResponseEntity();
        }
    }

    @ApiOperation(value = "修改${table_annotation}", notes = "修改${table_annotation}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "${model_name?uncap_first}Request",value ="${table_annotation}对象",required = true,dataType = "Update${model_name}Request")
    })
    @ApiResponses({
			@ApiResponse(code = CommonStatus.BAD_REQUEST, message = "Bad Request", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.NOT_FOUND, message = "Not Found", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.INTERNAL_SERVER_ERROR, message = "Server Error", response = MsgResponse.class) })
    @RequestMapping(value = "/${model_name?lower_case}s/{id}",
        method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<?> update${model_name}(@PathVariable String id,@Valid @RequestBody Update${model_name}Request ${model_name?uncap_first}Request,BindingResult bindingResult, HttpServletRequest request){
        if (bindingResult.hasErrors()){
            return  new ResponseEntity<>(
                new SysMessage(
                    bindingResult.getFieldError().getDefaultMessage()),HttpStatus.BAD_REQUEST);
        }
        try {
                return ${model_name?uncap_first}Service.update${model_name}(id,${model_name?uncap_first}Request, request);
            } catch (Exception e) {
               logger.error(e.getMessage(), e);
			   return new SysMessage(e).buildErrorResponseEntity();
            }
    }

    @ApiOperation(value = "删除${table_annotation}", notes = "删除${table_annotation}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value ="${table_annotation}ID",paramType = "path" ,required = true)
    })
    @ApiResponses({
			@ApiResponse(code = CommonStatus.BAD_REQUEST, message = "Bad Request", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.NOT_FOUND, message = "Not Found", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.INTERNAL_SERVER_ERROR, message = "Server Error", response = MsgResponse.class) })
    @RequestMapping(value = "/${model_name?lower_case}s/{id}",
            method = RequestMethod.DELETE)
    public HttpEntity<?> delete${model_name}(@PathVariable String id, HttpServletRequest request){
        try {
            return ${model_name?uncap_first}Service.delete${model_name}(id, request);
        } catch (Exception e) {
        	logger.error(e.getMessage(), e);
		    return new SysMessage(e).buildErrorResponseEntity();
        }
    }

    @ApiOperation(value = "查询${table_annotation}", notes = "查询${table_annotation}")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "keyword", value = "搜索关键字（关键字）", required = false, paramType = "query"),
        @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页码"),
        @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页记录数."),
        @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "排序字段")
    })
    @ApiResponses({ @ApiResponse(code = CommonStatus.OK, message = "OK", response = ${model_name}Response.class),
			@ApiResponse(code = CommonStatus.NO_CONTENT, message = "No Content", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.INTERNAL_SERVER_ERROR, message = "Server Error", response = MsgResponse.class) })
    @RequestMapping(value = "/${model_name?lower_case}s",
        method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<?> get${model_name}s(
    					@RequestParam(value = "keyword", required = false, defaultValue = "") String keyword,
                        @PageableDefault(page = 0, size = 20,sort = { "createTime" },
                        direction = Sort.Direction.ASC) Pageable pageable,
                        HttpServletRequest request){
        try {
            return ${model_name?uncap_first}Service.get${model_name}s(keyword, pageable, request);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
			return new SysMessage(e).buildErrorResponseEntity();
        }
    }
    @ApiOperation(value = "ID查询${table_annotation}", notes = "查询${table_annotation}详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id",value ="${table_annotation}ID",paramType = "path" ,required = true)
    })
    @ApiResponses({ @ApiResponse(code = CommonStatus.OK, message = "OK", response = ${model_name}Response.class),
			@ApiResponse(code = CommonStatus.NO_CONTENT, message = "No Content", response = MsgResponse.class),
			@ApiResponse(code = CommonStatus.INTERNAL_SERVER_ERROR, message = "Server Error", response = MsgResponse.class) })
    @RequestMapping(value = "/${model_name?lower_case}s/{id}",
                    method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public HttpEntity<?> get${model_name}ById(@PathVariable String id){
        try {
            return ${model_name?uncap_first}Service.get${model_name}ById(id);
         }catch (Exception e) {
            logger.error(e.getMessage(), e);
			return new SysMessage(e).buildErrorResponseEntity();
        }
     }

}